package org.example.aop.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.example.aop.annotation.PermissionAnnotation;
import org.example.aop.dto.request.UserRequest;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @Description:
 */
@Aspect
@Component
// 可以调整切面的顺序
@Order(0)
public class PermissionSecondAdvice {

    /**
     * ProceedingJoinPoint 继承自 JoinPoint，并增加了控制目标方法执行的能力，主要用于 Around 通知中。
     *
     * @param joinPoint
     * @param permissionAnnotation
     * @return
     * @throws Throwable
     */
    @Around(value = "@annotation(permissionAnnotation)", argNames = "joinPoint,permissionAnnotation")
    public Object permissionCheckFirst(ProceedingJoinPoint joinPoint, PermissionAnnotation permissionAnnotation) throws Throwable {
        System.out.println("===================第二个切面===================：" + System.currentTimeMillis());

        //获取请求参数，详见接口类
        Object[] objects = joinPoint.getArgs();
        // 这样会导致这个切面无法公共使用
        UserRequest userRequest = (UserRequest) objects[0];
        System.out.println(userRequest);

        // 执行目标方法
        return joinPoint.proceed();
    }
}
